package cc.deng.wubo.minapp.utils;

import org.nutz.lang.Files;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * 字符串工具类
 * @author java1234 小锋 老师
 *
 */
public class StringUtil {


	/**
	 * 将List集合转化成数据库支持的语法
	 * @param ids
	 * @return
	 */
	public static String dbIn(List<Long> ids){
		StringBuffer sb = new StringBuffer();
		ids.forEach(id->{
			sb.append(",").append(id);
		});
		sb.delete(0,1);
		return sb.toString();
	}

	/**
	 * 判断是否是空
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str){
		if(str==null||"".equals(str.trim())){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 判断是否不是空
	 * @param str
	 * @return
	 */
	public static boolean isNotEmpty(String str){
		if((str!=null)&&!"".equals(str.trim())){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 获取文件的后缀
	 */
	public static String suffix(MultipartFile file){
		String name = file.getOriginalFilename();
		return name.substring(name.lastIndexOf("."));
	}


	/**
	 *
	 */
	public static  List<String> namesLib(){
		String read = Files.read("config/chinese_character.txt");
		String lib = decodeUnicode(read);
		List<String> namesLib = Arrays.asList(lib.split(""));
		return namesLib;
	}

	/**
	 * 将unicode 转成汉字
	 * @param dataStr
	 * @return
	 */
	public static String decodeUnicode(final String dataStr) {
		int start = 0;
		int end = 0;
		final StringBuffer buffer = new StringBuffer();
		while (start > -1) {
			end = dataStr.indexOf("\\u", start + 2);
			String charStr = "";
			if (end == -1) {
				charStr = dataStr.substring(start + 2, dataStr.length());
			} else {
				charStr = dataStr.substring(start + 2, end);
			}
			char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
			buffer.append(new Character(letter).toString());
			start = end;
		}
		return buffer.toString();
	}


	public static Set<String> getAssistList(List<String> names, List<String> namesLib, int size, int tempSize, HashSet<String> result) {

		Set<String> tempResult = new HashSet<>();


		for(int i =0;i<tempSize;i++){
			Random random = new Random();
			String s = namesLib.get(random.nextInt(namesLib.size()));
			System.out.println(s);
			tempResult.add(s);
		}
		int size1 = result.size();
		result.addAll(tempResult);


		if (tempResult.size()<tempSize&&result.size()<(tempSize+size1)){
			// System.out.println("111111111111111111111111111111111111111111111");
			int tempsize = tempSize+size1-result.size();
			System.out.println(result.size());
			System.out.println(tempsize);
			getAssistList(names,namesLib,size,tempsize,result);
		}else{
			int i =0;
			for (String name:names){
				if (result.contains(name)){
					result.remove(name);
					i=i+1;
					//
				}
			}
			System.out.println(i);
			if (i>0){
				System.out.println("i="+i);
				getAssistList(names,namesLib,size,i,result);
			}

		}

		// names.addAll(result);
		return result;

	}

}
